angular.module('starter.sidemenu.services', [])

.provider('dummyData', function() {
  //status 0-hide 1-show
  var categories = [
    {id:1, title:"默认分类", status:1, isEdit:false},
    {id:2, title:".Net", status:1, isEdit:false},
    {id:3, title:"CSS & HTML", status:1, isEdit:false},
    {id:4, title:"Database", status:1, isEdit:false},
    {id:5, title:"Javascript", status:0, isEdit:false},
    {id:6, title:"Others", status:1, isEdit:false}
  ];

  var tags = [
    {id:1, title:"ionic", status:1, isEdit:false},
    {id:2, title:"日常", status:1, isEdit:true}
  ];

  var notes = [
    {id:1, categoryId:1, title:"下午有个会！", content:"下午有个会...", status:1, isStar:true, date:"2014/09/11", tagIds:[2]},
    {id:2, categoryId:3, title:"css圆角", content:"border-radius", status:1, isStar:true, date:"2014/07/11", tagIds:[1]},
    {id:3, categoryId:3, title:"css圆角2", content:"border-radius2", status:1, isStar:false, date:"2014/06/11", tagIds:[1]}
  ];
  
  // Some fake testing data
  var playlists = [
    { id: 1, title: 'Reggae', date: '2014/08/23'},
    { id: 3, title: 'Dubstep', date: '2014/09/29'},
    { id: 2, title: 'Chill', date: '2014/09/28'},
    { id: 7, title: 'Indie', date: '2014/08/22'},
    { id: 5, title: 'Rap', date: '2014/08/21'},
    { id: 6, title: 'Cowbell', date: '2013/08/23'},
  ];

  this.$get = function() {
      return {categories: categories, tags:tags, notes:notes, playlists: playlists};
  };

})

/**
 * A simple example service that returns some data.
 */
.factory('PlayListService', function($q, dummyData) {
  // Might use a resource here that returns a JSON array
  var playlists = dummyData.playlists;
  return {
    all: function(sortKey, status) {
      var deferred = $q.defer();
      var results = playlists.filter(function(element) {
        if (status === undefined) {
          return true;
        } else {
          return status === element.status;
        }    
      });
      results = results.sort(function(a, b){
         switch ( sortKey ) {
            case "date": 
              return a.date > b.date;
            case "title": 
              return a.title > b.title;
            default: 
              return parseInt(a.id) - parseInt(b.id);
         }
      });

      deferred.resolve(results);
      return deferred.promise;
    },
    get: function(playlistId) {
      var deferred = $q.defer();
      var results = playlists.filter(function(element) {
          return parseInt(playlistId) === element.id;
      });
      deferred.resolve(results);
      return deferred.promise;
    },
    findByTitle: function (titleKey) {
        var deferred = $q.defer();
        var results = playlists.filter(function(element) {
            return element.title.toLowerCase().indexOf(titleKey.toLowerCase()) == -1 ? false : true;
        });
        deferred.resolve(results);
        return deferred.promise;
    }
  }
})

.factory('CategoryListService', function($q, dummyData) {
  // Might use a resource here that returns a JSON array
  var categories = dummyData.categories;
  return {
    all: function(status, sortKey) {
      var deferred = $q.defer();
      var results = categories.filter(function(element) {
        if (status === undefined) {
          return true;
        } else {
          return status === element.status;
        }    
      });
      results = results.sort(function(a, b){
         switch ( sortKey ) {
            case "title": 
              return a.title > b.title;
            default: 
              return parseInt(a.id) - parseInt(b.id);
         }
      });

      deferred.resolve(results);
      return deferred.promise;
    },
    get: function(categoryId) {
      var deferred = $q.defer();
      var results = categories.filter(function(element) {
          return parseInt(categoryId) === element.id;
      });
      deferred.resolve(results);
      return deferred.promise;
    }
  };
})

.factory('TagListService', function($q, dummyData) {
  // Might use a resource here that returns a JSON array
  var tags = dummyData.tags;
  return {
    all: function(status, sortKey) {
      var deferred = $q.defer();
      var results = tags.filter(function(element) {
        if (status === undefined) {
          return true;
        } else {
          return status === element.status;
        }    
      });
      results = results.sort(function(a, b){
         switch ( sortKey ) {
            case "title": 
              return a.title > b.title;
            default: 
              return parseInt(a.id) - parseInt(b.id);
         }
      });

      deferred.resolve(results);
      return deferred.promise;
    },
    get: function(tagId) {
      var deferred = $q.defer();
      var results = tags.filter(function(element) {
          return parseInt(tagId) === element.id;
      });
      deferred.resolve(results);
      return deferred.promise;
    }
  };
})

.factory('NoteListService', function($q, dummyData) {
  // Might use a resource here that returns a JSON array
  var notes = dummyData.notes;
  return {
    all: function(status, sortKey) {
      var deferred = $q.defer();
      var results = notes.filter(function(element) {
        if (status === undefined) {
          return true;
        } else {
          return status === element.status;
        }    
      });
      results = results.sort(function(a, b){
         switch ( sortKey ) {
            case "date": 
              return a.date > b.date;
            case "title": 
              return a.title > b.title;
            default: 
              return parseInt(a.id) - parseInt(b.id);
         }
      });

      deferred.resolve(results);
      return deferred.promise;
    },
    getToCategory: function(categoryIds, sortKey) {
      var deferred = $q.defer();
      var categories = dummyData.categories;
      var results = categories.filter(function(element) {        
        var isInclude = false;
        if(categoryIds == 0){
          isInclude = element.status==1;
        }
        else{
          for (var i = categoryIds.length - 1; i >= 0; i--) {
            if(parseInt(categoryIds[i])==parseInt(element.id)){
              isInclude = true;
              break;
            }
          }
        }

        if(isInclude){
          var notes_results = notes.filter(function(note) {
              return (parseInt(element.id) === note.categoryId) && element.status==1;                
          });
          element.notelist = notes_results.sort(function(a, b){
             switch ( sortKey ) {
                case "date": 
                  return a.date > b.date;
                case "title": 
                  return a.title > b.title;
                default: 
                  return parseInt(a.id) - parseInt(b.id);
             }
          });
          isInclude = element.notelist.length>0;
        }
        return isInclude;      
      });
      deferred.resolve(results);
      return deferred.promise;
    },
    get: function(noteId) {
      var deferred = $q.defer();
      var results = notes.filter(function(element) {
          return parseInt(noteId) === element.id;
      });
      deferred.resolve(results);
      return deferred.promise;
    }
  };
})

;
